﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CaptchaLearning.Infrastructure.Service
{
    public static class LogService
    {
        private const String _logPath = "Log.txt";
        private const Boolean _debugMode = true;

        /// <summary>
        /// Description: Write log to _logPath. If debug mode opened -> messageBox that err
        /// Created by: Nam Mai
        /// Created date: 9/17/2012
        /// </summary>
        public static void WriteLog(String file, String method, String exception)
        {
            String errMsg = "";
            errMsg = "[" + String.Format("{0:dd-mm-yyyy}", DateTime.Now) + "]";
            errMsg += "[" + file + "]";
            errMsg += "[" + method + "]";
            errMsg += ": " + exception;
            if (_debugMode)
            {
                MessageBox.Show(errMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            StreamWriter log;

            if (!File.Exists(_logPath))
                log = new StreamWriter(_logPath);
            else
                log = File.AppendText(_logPath);
            log.WriteLine(errMsg);

            log.Close();
        }
    }
}
